﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hjp.FrameWork.Common;
using Wjtour.Domain;
using Wjtour.BLL;

namespace Wjtour.Web.Admin.menu
{
    public partial class addedi : BasePage
    {
        protected TextBox title;
        protected TextBox node;
        protected RadioButtonList hide;
        protected TextBox mod;
        protected TextBox act;
        protected TextBox sort;
        protected Button submit;
        protected int id = 0;
        Wjtour.Domain.menu entity = new Domain.menu();
        menu_bll _bll = new menu_bll();
        private List<Wjtour.Domain.menu> menus = new List<Domain.menu>();
        protected DropDownList node_id;

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Utils.StrToInt(Request["id"], 0);
            submit.Click += new EventHandler(submit_Click);
            if (id > 0)
            {
                entity = _bll.Load(id);
                if (!IsPostBack)
                {
                    BindDropdown();
                    if (entity != null)
                    {
                        title.Text = entity.title;
                        node.Text = entity.node;
                        hide.SelectedValue = entity.hide.ToString();
                        mod.Text = entity.mod;
                        act.Text = entity.act;
                        sort.Text = entity.sort.ToString();
                    }
                }
            }
        }

        void BindDropdown()
        {
            menus = _bll.GetAll<Wjtour.Domain.menu>(@" select * from menu order by listorder desc").ToList();

            foreach (var item in menus.Where(m => m.parent_id == 0).ToList())
            {
                node_id.Items.Add(new ListItem(item.title, item.id.ToString()));
            }
        }

        void submit_Click(object sender, EventArgs e)
        {
            entity.id = id;
            entity.title = title.Text;
            entity.node = node.Text;
            entity.hide = Utils.StrToInt(hide.SelectedValue, 0);
            entity.mod = mod.Text;
            entity.act = act.Text;
            entity.sort = Utils.StrToInt(sort.Text, 0);

            bool b = _bll.Save(entity);
            if (b)
            {
                SaveLog(id == 0 ? "Add" : "edit");
                Utils.Alert(this.Page, "保存成功！");
                Utils.Redirect(this.Page, "list.aspx", false);
            }
        }
    }
}